/**
 * NetUitl 网络请求的实现
 * https://github.com/facebook/react-native
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
    Platform,
    AsyncStorage
} from 'react-native';


class NetUitl extends React.Component{
    static post(url,params,success,noToken,fail) {
        var token = '';
        try {
            AsyncStorage.getItem(
                'token',
                (error, result)=> {
                    if (error) {
                        alert('取值失败:' + error);
                    } else {
                        // alert('取值成功:'+result);
                        this.token = result
                    }
                }
            )
        } catch (error) {
            alert('失败' + error);
        }
        var fetchOptions = {
            method: 'POST',
            headers: {
                'token': this.token
            },
            body: params,
        };
        fetch(url, fetchOptions)
            .then((response) => response.json())
            .then((json) => {
                if (json.status == 200) {
                   success(json)
                } else if (json.status == 301) {
                    noToken()
                } else {
                    fail()
                }
            }).done();

    }

}

module.exports = NetUitl;